package funtastic.spellingbee;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ExamAdapter extends ArrayAdapter<SpellingDetails>
{
    private Context               mContext;
    private List<SpellingDetails> mSpellingDetails;
    private TextToSpeech          mTextToSpeech;
    private int                   mTextViewResourceId;

    public ExamAdapter(Context context, int textViewResourceId, List<SpellingDetails> spellingDetails, TextToSpeech textToSpeech)
    {
        super(context, textViewResourceId, spellingDetails);

        mContext = context;
        mTextViewResourceId = textViewResourceId;
        mSpellingDetails = spellingDetails;
        mTextToSpeech = textToSpeech;

        for (int index = 0; index < mSpellingDetails.size(); index++)
        {
            mSpellingDetails.get(index).setSpellingUser("");
            mSpellingDetails.get(index).setVerified(false);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View rowView = convertView;

        if (rowView == null)
        {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            rowView = inflater.inflate(mTextViewResourceId, parent, false);
        }

        int currentPosition = (rowView.getTag() == null) ? -1 : ((Integer) rowView.getTag()).intValue();
        if (currentPosition != position)
        {
            Button buttonHint = (Button) rowView.findViewById(R.id.buttonHint);
            buttonHint.setTag(mSpellingDetails.get(position).getSentense());

            TextView textViewSpellingUser = (TextView) rowView.findViewById(R.id.editTextSpellingUser);
            textViewSpellingUser.setOnTouchListener(new OnTouchListener()
            {
                @Override
                public boolean onTouch(View view, MotionEvent event)
                {
                    if (event.getAction() == MotionEvent.ACTION_UP)
                    {
                        mTextToSpeech.speak(view.getTag().toString(), TextToSpeech.QUEUE_ADD, null);
                    }
                    return false;
                }
            });
            textViewSpellingUser.setOnFocusChangeListener(new OnFocusChangeListener()
            {
                @Override
                public void onFocusChange(View view, boolean hasFocus)
                {
                    TextView textViewSpellingUser = (TextView) view;
                    LinearLayout linearLayout = (LinearLayout) view.getParent();
                    int position = (linearLayout.getTag() == null) ? -1 : ((Integer) linearLayout.getTag()).intValue();
                    if (position != -1)
                    {
                        mSpellingDetails.get(position).setSpellingUser(textViewSpellingUser.getText().toString());
                        if (mSpellingDetails.get(position).getVerified())
                        {
                            if (mSpellingDetails.get(position).isCorrect())
                            {
                                textViewSpellingUser.setBackgroundResource(R.drawable.textfield_correct_green);
                            }
                            else
                            {
                                textViewSpellingUser.setBackgroundResource(R.drawable.textfield_error_red);
                            }
                        }
                        else
                        {
                            textViewSpellingUser.setBackgroundResource(android.R.drawable.editbox_background_normal);
                        }
                    }
                }
            });
            textViewSpellingUser.setTag(mSpellingDetails.get(position).getSpelling());
            textViewSpellingUser.setText(mSpellingDetails.get(position).getSpellingUser());
            if (mSpellingDetails.get(position).getVerified())
            {
                if (mSpellingDetails.get(position).isCorrect())
                {
                    textViewSpellingUser.setBackgroundResource(R.drawable.textfield_correct_green);
                }
                else
                {
                    textViewSpellingUser.setBackgroundResource(R.drawable.textfield_error_red);
                }
            }
            else
            {
                textViewSpellingUser.setBackgroundResource(android.R.drawable.editbox_background_normal);
            }

            rowView.setTag(position);
        }

        return rowView;
    }

}
